import Enemy from './Enemy.js'
import Bullet from './Bullet.js'
import Boom from './Boom.js'

class Pool {
  constructor (ctx, hero, music) {
    this.ctx = ctx
    this.hero = hero
    this.score = 0
    this.music = music
    this.enemys = []
    this.bullets = []
    this.booms = []
    this.animations = []
    for(let i = 1; i <=19; i++){
      let img = new Image()
      img.src = 'images/explosion'+ i +'.png'
      this.animations.push(img)
    }
  }
  drawEnemys (top) {
    if(top % 50 == 0){
      const enemy = new Enemy(this.ctx)
      this.enemys.push(enemy)
    }
    this.enemys = this.enemys.filter(item => {
      return item.status === true
    })
    this.enemys.forEach(enemy => {
      enemy.draw()
      this.hero.gameOver(enemy)
      this.bullets.forEach(bullet => {
        let flag =  enemy.isBoom(bullet)
        if(flag) {
          this.score ++
          bullet.status = false
          let boom = new Boom(this.ctx, enemy)
          this.booms.push(boom) 
        }
      })
    })
  }
  drawBullets (top) {
    if(top % 20 === 0){
      let bullet = new Bullet(this.ctx, this.hero)
      this.bullets.push(bullet)
      this.music.bulletPlay()
    }
    this.bullets = this.bullets.filter(item => {
      return item.status === true
    })
    this.bullets.forEach(bullet => {
      bullet.draw()
    })
    
  }
  drawBooms () {
    this.booms = this.booms.filter(item => {
      return item.status === true
    })
    this.booms.forEach(boom => {
      this.music.boomPlay()
      boom.draw(this.animations)
    })
  }
}
export default Pool